/**
 * 此指令试用时候必须在input标签后面。并且不能间隔超过2个以上标签。不能被包裹在label标签中（会获取不到焦点）。
 * <input class="padding-left-right-10  inline" ng-model="data.value" type="tel" maxlength="11"/>
 * <password-cover></password-cover>
 * created by ZCP
 */
APP.directive('passwordCover', ['$rootScope', '$state', '$timeout',
  function ($rootScope, $state, $timeout) {
    return {
      restrict: 'E',
      replace: true,
      template: '<i class="icon ion-eye-disabled" style="font-size:18px;color: #660000;vertical-align: middle;padding: 5px 10px 5px 0;;background:#fff;margin-left: -4px;"></i>',
      link: function (scope, element, attr) {
        var flag = false;
        var input = element[0].previousElementSibling;
        if (input.nodeName != 'INPUT') {
          input = element[0].previousElementSibling.previousElementSibling;
        }
        if (input.nodeName != 'INPUT') {
          input = element[0].previousElementSibling.previousElementSibling.previousElementSibling;
        }
        if (input.nodeName != 'INPUT') {
          throw('password-cover必需跟在input标签后，间隔不超过3个！！');
        }
        element.on('click', function () {
          var c = element[0].getAttribute('class');
          if (!c) {
            c = '';
          }
          if (flag) {
            c = c.replace('ion-eye', 'ion-eye-disabled');
            input.setAttribute('type', 'password');
          } else {
            c = c.replace('ion-eye-disabled', 'ion-eye');
            input.setAttribute('type', 'text');
          }
          flag = !flag;
          element[0].setAttribute('class', c);
        });

      }
    }
  }]);
